//
// Created by chao.li on 2023/9/11.
//

#ifndef SRC_IMAGE_H
#define SRC_IMAGE_H

#include <string>
#include <iostream>

#include "color.h"

class image
{
    float *buffer;
    const unsigned int COLOR_SPACE_BITES = 256;
    int to_output(float f) const;
public:
    int width,height;

public:
    image(int width, int height);

    template<class data_type>
    bool write_color(int x, int y, data_type color)
    {
        if(x >= 0 && x < width && y >= 0 && y < height){
            auto index = (y * width + x) * 3;
            buffer[index] = color[0];
            buffer[index + 1] = color[1];
            buffer[index + 2] = color[2];
            return true;
        } else{
            return false;
        }
    }

    template<class data_type>
    bool write_color(const vec2& p, data_type color)
    {
        return write_color(p.x, p.y, color);
    }

    void output_ppm(const std::string& path);

    void output_png(const std::string& path);

    ~image();
};

#endif //SRC_IMAGE_H
